<?php namespace Common\Model;
use Hdphp\Model\Model;
class CateGory extends Model{
	protected $table='category';
	//自动验证
	protected $validate=array(
		//array(字段名,验证方法,错误信息,验证条件,验证时间)
		array('cname','required','分类名称不能为空',3,3)
		);
	
	//分类添加	
	public function store(){
		if($this->create()){
			$this->add();
			return true;
		}
		return false;
	}
	//修改
	public function edit(){
		if(!$this->create()) return false;
		//修改，需要在页面加隐藏域
		$this->save();
		return true;
	}
	
	//获得除了自己和自己子集的数据
	public function getNoMy($cid){
		//获得自己的子集的cid
		$cids=$this->getSon($this->get(),$cid);
		//压入自己的Cid
		$cids[]= $cid;
		//排除自己和自己子集的cid数据
		//SELECT * FROM shop_category WHERE cid NOT(2,3,5);
		$where = "cid NOT IN(" . implode(',', $cids) . ")";
		return $this->where($where)->get();
	}
	
	
	//获得子集
	public function getSon($data,$cid){
		$temp = array();
		//循环所有数据
		foreach($data as $v){
			//如果找到当前分类的子集
			if($v['pid'] == $cid){
				//压入到临时数组
				$temp[]= $v['cid'];
				//递归调用不断合并
				$temp = array_merge($temp,$this->getSon($data,$v['cid']));
			}
		}
		//得到最终结果 返出
		return $temp;
	}
	
	public function getAll(){
		$data = $this->get();
		return Data::tree($data,'cname');
	}	
}





 ?>